package com.itheima.controller.impl;

import com.itheima.controller.lnter.IteacherCon;
import com.itheima.damain.Teacher;
import com.itheima.service.impl.TeacherServicelmpl;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherConlmpl implements IteacherCon {

    private TeacherServicelmpl teacherServicelmpl = new TeacherServicelmpl();

    private Scanner sc = new Scanner(System.in);

    @Override
    public void stiat() {

        //死循环
        loop:
        while (true) {
            // 键盘录入
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            //根据不同的选择做不同的事
            switch (choice) {
                case "1":
                    //System.out.println("添加成功");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除成功");
                    deleteTeacher();
                    break;
                case "3":
                    // System.out.println("修改成功");
                    updateTeacher();
                    break;
                case "4":
                    // System.out.println("查看成功");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("退出老师系统");
                    // break loop;
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;

            }
        }
    }

    @Override // 添加老师
    public void addTeacher() {
        String sid = null;
        while (true) {
            System.out.println("输入学号");
            sid = sc.next();
            boolean falg = teacherServicelmpl.isExtsis(sid);
            if (falg) {
                System.out.println("学号已存在");
            } else {
                break;
            }
        }
        Teacher teacher = getTeacher(sid);
        teacherServicelmpl.addTeacher(teacher);
        System.out.println("添加成功");
    }

    @Override // 删除老师
    public void deleteTeacher() {
        System.out.println("输入老师id");
        String sid = sc.next();
        boolean falg = teacherServicelmpl.isExtsis(sid);
        if (falg) {
            teacherServicelmpl.deleteTeacher(sid);
            System.out.println("删除成功");
        } else {
            System.out.println("老师不存在");
        }
    }

    @Override // 修改老师
    public void updateTeacher() {
        System.out.println("输入要修改的id");
        String sid = sc.next();
        boolean falg = teacherServicelmpl.isExtsis(sid);
        if (falg) {
            Teacher teacher = getTeacher(sid);
            teacherServicelmpl.updateTeacher(sid, teacher);
            System.out.println("修改成功");
        } else {
            System.out.println("老师不存在");
        }
    }

    @Override // 查看老师
    public void findAllTeacher() {
        ArrayList<Teacher> tea = teacherServicelmpl.findAllTeacher();
        if (tea == null || tea.size() < 0) {
            System.out.println("无信息,输入后查看");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < tea.size(); i++) {
            Teacher t = tea.get(i);
            if (t != null) {
                System.out.println(t);
            }
        }
    }

    //添加老师和修改老师
    private Teacher getTeacher(String sid) {
        System.out.println("输入姓名");
        String name = sc.next();
        System.out.println("输入年龄");
        int age = sc.nextInt();
        System.out.println("输入生日");
        String birthday = sc.next();
        return new Teacher(sid, name, age, birthday);
    }
}
